package com.maidada.mddaichat.util;

import com.maidada.mddaichat.common.ErrorCode;
import com.maidada.mddaichat.exception.BusinessException;

import javax.servlet.http.HttpServletRequest;

/**
 * @author wulinxuan
 * @date 2025-04-17 09:46
 */
public class CommonUtils {

    public static String getTokenByRequest() {
        HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
        String token = request.getHeader("X-Access-Token");
        if (token == null || token.isEmpty()) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        return token;
    }

    public static String getUserIdByRequest() {
        String token = getTokenByRequest();
        return JwtUtils.getUserIdFromToken(token);
    }
}
